package com.raidpoints.client;

import java.util.ArrayList;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.TextBox;

public class TabEditor extends DialogBox {

	private ListBox gameBox;
	private ListBox groupBox;
	private Button saveButton;
	private Button cancelButton;
	RaidPoints rp;
	private TextBox nameBox;
	private Button removeButton;
	private CheckBox raidBox;
	private CheckBox ueBox;

	public TabEditor(final int tabnum, RaidPoints mrp) {
		if(tabnum >= 0) setHTML("Tab Editor");
		else setHTML("New Tab");
		rp = mrp;

		LayoutPanel layoutPanel = new LayoutPanel();
		setWidget(layoutPanel);
		layoutPanel.setSize("359px", "255px");

		Label lblGames = new Label("Games:");
		layoutPanel.add(lblGames);
		layoutPanel.setWidgetLeftWidth(lblGames, 14.0, Unit.PX, 56.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblGames, 74.0, Unit.PX, 16.0, Unit.PX);

		Label lblGroups = new Label("Groups:");
		layoutPanel.add(lblGroups);
		layoutPanel.setWidgetLeftWidth(lblGroups, 186.0, Unit.PX, 56.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblGroups, 74.0, Unit.PX, 16.0, Unit.PX);

		gameBox = new ListBox();
		gameBox.setMultipleSelect(true);
		gameBox.setSize("156px", "111px");
		layoutPanel.add(gameBox);
		layoutPanel.setWidgetLeftWidth(gameBox, 14.0, Unit.PX, 156.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(gameBox, 96.0, Unit.PX, 111.0, Unit.PX);
		gameBox.setVisibleItemCount(5);

		groupBox = new ListBox();
		groupBox.setMultipleSelect(true);
		groupBox.setSize("156px", "111px");
		layoutPanel.add(groupBox);
		layoutPanel.setWidgetLeftWidth(groupBox, 186.0, Unit.PX, 156.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(groupBox, 96.0, Unit.PX, 111.0, Unit.PX);
		groupBox.setVisibleItemCount(5);

		saveButton = new Button("Save");
		layoutPanel.add(saveButton);
		layoutPanel.setWidgetLeftWidth(saveButton, 47.0, Unit.PX, 81.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(saveButton, 213.0, Unit.PX, 28.0, Unit.PX);

		cancelButton = new Button("Cancel");
		layoutPanel.add(cancelButton);
		layoutPanel.setWidgetLeftWidth(cancelButton, 221.0, Unit.PX, 81.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(cancelButton, 213.0, Unit.PX, 28.0, Unit.PX);

		raidBox = new CheckBox("Raids");
		layoutPanel.add(raidBox);
		layoutPanel.setWidgetLeftWidth(raidBox, 24.0, Unit.PX, 81.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(raidBox, 49.0, Unit.PX, 19.0, Unit.PX);

		ueBox = new CheckBox("User Events");
		layoutPanel.add(ueBox);
		layoutPanel.setWidgetLeftWidth(ueBox, 94.0, Unit.PX, 109.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(ueBox, 49.0, Unit.PX, 19.0, Unit.PX);

		Label lblTabName = new Label("Tab Name:");
		layoutPanel.add(lblTabName);
		layoutPanel.setWidgetLeftWidth(lblTabName, 14.0, Unit.PX, 75.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblTabName, 18.0, Unit.PX, 16.0, Unit.PX);

		nameBox = new TextBox();
		layoutPanel.add(nameBox);
		layoutPanel.setWidgetLeftWidth(nameBox, 105.0, Unit.PX, 185.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(nameBox, 2.0, Unit.PX, 32.0, Unit.PX);

		removeButton = new Button("Remove");
		layoutPanel.add(removeButton);
		layoutPanel.setWidgetLeftWidth(removeButton, 134.0, Unit.PX, 81.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(removeButton, 213.0, Unit.PX, 28.0, Unit.PX);
		removeButton.setVisible(false);

		//tabname:<>:raids(y/n):<>:user(y/n):<>:games:<>:groups
		
		cancelButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				hide();				
			}
		});
		
		saveButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				if(nameBox.getText().equals("") || !nameBox.getText().matches("[a-zA-Z0-9 ]*")){
					Window.alert("Invalid tab name");
					return;
				}
				StringBuilder tstr = new StringBuilder();	
				tstr.append(nameBox.getText() + ":<>:");
				tstr.append((raidBox.getValue() ? "Y" : "N") + ":<>:");
				tstr.append((ueBox.getValue() ? "Y" : "N") + ":<>:!");
				for(int i = 0; i < gameBox.getItemCount(); i++){
					if(gameBox.isItemSelected(i)) tstr.append(gameBox.getItemText(i) + "[]");
				}
				tstr.append(":<>:!");
				for(int i = 0; i < groupBox.getItemCount(); i++){
					if(groupBox.isItemSelected(i)) tstr.append(groupBox.getItemText(i) + "[]");
				}
				if(tabnum > 0){
					rp.tablist.set(tabnum, tstr.toString());
					rp.currtab = tabnum;
				}
				else{
					rp.tablist.add(tstr.toString());
					rp.currtab = rp.tablist.size() - 1;
				}
				if(rp.navmap.get(rp.eventItem) instanceof RaidCalendar){
					 RaidCalendar trp = (RaidCalendar) rp.navmap.get(rp.eventItem);
					 trp.SetupTabs();
					 trp.tabbar.selectTab(rp.currtab);
				 }
				hide();
			}
		});
		
		removeButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				if(tabnum < 0) return;
				rp.tablist.remove(tabnum);
				rp.currtab = 0;
				if(rp.navmap.get(rp.eventItem) instanceof RaidCalendar){
					 RaidCalendar trp = (RaidCalendar) rp.navmap.get(rp.eventItem);
					 trp.SetupTabs();
					 trp.tabbar.selectTab(rp.currtab);
				 }
				hide();				
			}
		});
		

		AsyncCallback<ArrayList<String>> tinfoCallback = new AsyncCallback<ArrayList<String>>(){

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onSuccess(ArrayList<String> result) {
				boolean atg = true;
				if(tabnum >= 0){
					String[] split = rp.tablist.get(tabnum).split(":<>:");
					nameBox.setText(split[0]); nameBox.setEnabled(false);
					removeButton.setVisible(true);
					raidBox.setValue(split[1].equals("Y"));
					ueBox.setValue(split[2].equals("Y"));
					String games = split[3];
					String groups = split[4];
					for(int i = 0; i < result.size(); i++){
						String item = result.get(i);
						if(item.equals(":<>:")){
							atg = false; continue;
						}
						if(atg){
							gameBox.addItem(item);
							if(games.contains(item)) gameBox.setItemSelected(gameBox.getItemCount()-1,true);
						}
						else{
							groupBox.addItem(item);
							if(groups.contains(item)) groupBox.setItemSelected(groupBox.getItemCount()-1,true);
						}
					}
				}
				else{
					for(int i = 0; i < result.size(); i++){
						String item = result.get(i);
						if(item.equals(":<>:")){
							atg = false; continue;
						}
						if(atg)	gameBox.addItem(item);
						else groupBox.addItem(item);
					}
				}
			}
		};
		rp.rpsvc.GetTabInfo(tinfoCallback);
	}
}
